#Binary parser template

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class TouhouSkyArena_MDL(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(TouhouSkyArena_MDL,self).__init__("MDL")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def read_name(self):

        string = ""
        char = self.inFile.read_char()
        while char != "\x00":
            string = ''.join([string, char])
            char = self.inFile.read_char()
        return string
    
    def parse_material(self):
        
        self.inFile.read_long(9)
        matName = self.read_name()
        self.inFile.seek(84, 1)
        
        charLen = self.inFile.read_long()
        texName = os.path.basename(self.inFile.read_string(charLen)[:-1])
        self.create_material(0)
        self.add_material_name(0, matName)
        self.add_texture_name(0, texName)
    
    def parse_vertices(self, numVerts, meshName):
        
        print "%d vertices" %numVerts
        for i in range(numVerts):
            
            tu, tv = self.inFile.read_float(2)
            vx, vy, vz = self.inFile.read_float(3)
            w1, w2, w3, w4 = self.inFile.read_float(4)
            
            nx, ny, nz = self.inFile.read_float(3)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
    
    def parse_faces(self, meshName):
        
        numFaces = self.inFile.read_long() / 3
        print "%d faces" %numFaces
        for i in range(numFaces):
            v1, v2, v3 = self.inFile.read_long(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        magic = self.inFile.read_string(23)
        null, unk1 = self.inFile.read_long(2)
        meshName = self.read_name()        
        unk2, unk3, numVerts, unk4, null = self.inFile.read_long(5)
        
        self.create_object(meshName)
        self.parse_vertices(numVerts, meshName)
        self.inFile.read_long(2)
        self.parse_faces(meshName)
        self.parse_material()
        
        self.inFile.seek(16, 1)
        unk5, numBytes = self.inFile.read_long(2)
        self.inFile.seek(numBytes, 1)
        self.inFile.seek(4, 1)
        
        #EOF
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = TouhouSkyArena_MDL(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "MBS", "MDL", "Touhou Sky Arena MDL"

if __name__ == '__main__':
    
    file1 = "Stage_01_croud_01_model.mdl"
    file2 = "Stage_07_sky_model.mdl"
    file3 = "Stage_07_sakura.mdl"
    obj = read_file(file3)